package com.lykj.llw.box.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;

import androidx.annotation.Nullable;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.lykj.lib_base.component.GlideApp;
import com.lykj.llw.box.inter.onBitmapDownloadCompleteListener;

public class ImageGetterUtils {
    public static class MyImageGetter implements Html.ImageGetter {
        private onBitmapDownloadCompleteListener mListener;
        private URLDrawable urlDrawable = null;
        private Context context;
        private int mCount = 0;
        private int mOutSideCount = 0;

        public MyImageGetter(Context context, onBitmapDownloadCompleteListener listener, int count) {
            this.context = context;
            this.mListener = listener;
            mCount = 0;
            mOutSideCount = count;
        }

        @Override
        public Drawable getDrawable(final String source) {
            urlDrawable = new URLDrawable();
            GlideApp.with(context).asBitmap().load(source).listener(
                    new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            Log.e("img", e.toString());
                            mCount++;
                            if (mCount == mOutSideCount) {
                                mListener.onComplete();
                            }
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            urlDrawable.bitmap = changeBitmapSize(resource);
                            urlDrawable.setBounds(0,
                                    0,
                                    changeBitmapSize(resource).getWidth(),
                                    changeBitmapSize(resource).getHeight());
                            mCount++;
                            if (mCount == mOutSideCount) {
                                mListener.onComplete();
                            }
                            return true;
                        }
                    }
            ).submit();
            return urlDrawable;
        }

        public class URLDrawable extends BitmapDrawable {
            public Bitmap bitmap;

            @Override
            public void draw(Canvas canvas) {
                super.draw(canvas);
                if (bitmap != null) {
                    canvas.drawBitmap(bitmap, 0, 0, getPaint());
                }
            }
        }

        private Bitmap changeBitmapSize(Bitmap bitmap) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
//            Log.e("width", "width:" + width);
//            Log.e("height", "height:" + height);
            //设置想要的大小
            int newWidth = width;
            int newHeight = height;
            //计算压缩的比率
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            //获取想要缩放的matrix
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            //获取新的bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
//            bitmap.getWidth();
//            bitmap.getHeight();

//            Log.e("newWidth", "newWidth" + bitmap.getWidth());
//            Log.e("newHeight", "newHeight" + bitmap.getHeight());
            return bitmap;
        }
    }
}
